import { api } from "src/boot/axios";
import { useRouter } from "vue-router";
const router = useRouter()
export default {
    requestMethod(url, method, query, param) {
        function changeParamToString(param) {
            if (param == undefined) return ''
            let re = '?'
            let keys = Object.keys(param)
            for (let i = 0; i < keys.length; i++) {
                re += keys[i] + '=' + param[keys[i]].toString()
            }
            return re
        }
        return new Promise(async (resolve, reject) => {
            api[method](url + changeParamToString(param), query).then(res => {
                resolve(res)
            })
        })
    },
    changeString(url) {
        url = url.split('')
        for (let i = 0; i < url.length; i++) {
            if (url[i] == '?') {
                url[i] = '!'
            }
            if (url[i] == '&') {
                url[i] = '*'
            }
        }
        let re = ''
        for (let i = 0; i < url.length; i++) {
            re += url[i]
        }
        return re
    },
    async tableRowClick(bodyInfo, otherBodyInfo) {
        let userInfo = JSON.parse(localStorage.getItem('loginedUserInfo'))

        let otherEntityCode = otherBodyInfo.otherEntityCode
        let otherEntityType = otherBodyInfo.otherEntityType
        let eventCode = bodyInfo.eventCode

        let paramList = {
            companyId: userInfo.companyId,
            entityCode: bodyInfo.entityCode,
            entityType: bodyInfo.entityType,
            // eventCode: bodyInfo.eventCode,
            // otherEntityCode: otherBodyInfo.otherEntityCode,
            // otherEntityType: otherBodyInfo.otherEntityType,
            sceneCode: '01' + '102' + userInfo.companyId + bodyInfo.entityCode + bodyInfo.entityType + '001' + userInfo.roleType,
            //     01是PC渠道 102是主体首页                                                             001查询
            groupEntityCode: "101",
            objectCodeType: "109"
        }

        // let paramList = {
        //   companyId: userInfo.companyId,
        //   entityCode: bodyInfo.entityCode,
        //   entityType: bodyInfo.entityType,
        //   groupEntityCode: "101",
        //   objectCodeType: "109",
        //   sceneCode: "01500430030100104"
        // }

        // param.objectCodeType = '109'
        let re3 = await this.requestMethod('/channel/channel/queryMenu1', 'post', paramList)
        if (re3.data.status != 'SUCCESS') {
            console.log('queryMenu1 falied', re3)
            return 'queryMenu1 falie'
        }
        let queryMenuResData = re3.data.data

        // let queryMenuResData = [
        //   {
        //     "groupEntityCode": "101",
        //     "companyId": "50043",
        //     "propertyCode": "10003",
        //     "entityCode": "001",
        //     "objectCode": "5004300301109",
        //     "propertyName": "样板",
        //     "entityType": "109",
        //     "typeName": "首页菜单",
        //     "codeName": "操作首页",
        //     "id": "0627170651180001101500431",
        //     "gmtCreate": "2024-05-22T03:29:09.000+00:00"
        //   },
        //   {
        //     "groupEntityCode": "101",
        //     "companyId": "50043",
        //     "propertyCode": "00311",
        //     "entityCode": "001",
        //     "objectCode": "5004300301109",
        //     "propertyName": "样式",
        //     "entityType": "109",
        //     "typeName": "首页菜单",
        //     "codeName": "操作首页",
        //     "id": "618215536765001101500431",
        //     "gmtCreate": "2024-05-22T03:29:09.000+00:00"
        //   },
        //   {
        //     "groupEntityCode": "101",
        //     "companyId": "50043",
        //     "propertyCode": "00204",
        //     "entityCode": "001",
        //     "objectCode": "5004300301109",
        //     "propertyName": "样例",
        //     "entityType": "109",
        //     "typeName": "首页菜单",
        //     "codeName": "操作首页",
        //     "id": "D061826765001101500431",
        //     "gmtCreate": "2024-05-22T03:29:09.000+00:00"
        //   },
        // ]

        let urls = []

        for (let i = 0; i < queryMenuResData.length; i++) {

            let other_entityCode = queryMenuResData[i].propertyCode.slice(0, 3)
            let other_entityName = '主体名称'
            let other_entityType = queryMenuResData[i].propertyCode.slice(3, 5)
            let other_typeName = queryMenuResData[i].propertyName
            let eventCode = '001'
            //组织page_str
            // let page_str = "entityCode=" + other_entityCode + "&entityType=" + other_entityType + "&eventCode=" + eventCode + "&entityName=" + other_entityName + "&typeName=" + other_typeName
            let page_str = "entityCode=" + other_entityCode + "&entityType=" + other_entityType + "&typeName=" + other_typeName

            let paramRoute = {
                companyId: userInfo.companyId,
                entityCode: bodyInfo.entityCode,
                entityType: bodyInfo.entityType,
                sceneCode: '01' + userInfo.companyId + bodyInfo.entityCode + bodyInfo.entityType + eventCode + userInfo.roleType + other_entityCode + other_entityType,
                groupEntityCode: "101",
                objectCodeType: "130"
            }

            let route_datas = []
            let route_str = ''

            let re1 = await this.requestMethod('/channel/channel/fetchChannelRoute2', 'post', paramRoute)
            if (re1.data.status != 'SUCCESS') {
                console.log('fetchChannelRoute2 failed')
                continue
            } else {

            }
            route_datas.push(re1.data.data)

            for (let ix = 0; ix < route_datas.length; ix++) {
                //组织数据
                let route_data = route_datas[ix]
                let otherPropertyEnglish = ''
                let propertyEnglish = ''
                let entityCode1 = ''
                let entityType1 = ''
                let source_entity = bodyInfo
                let entity_user = userInfo

                //组织route_str
                if (route_data == null) {

                } else {
                    for (let i = 0; i < route_data.length; i++) {
                        propertyEnglish = route_data[i].propertyEnglish
                        otherPropertyEnglish = route_data[i].otherPropertyEnglish
                        entityCode1 = route_data[i].otherEntityCode
                        entityType1 = route_data[i].otherEntityType

                        if (entityCode1 == other_entityCode && entityType1 == other_entityType) {
                            for (let item in source_entity) {
                                if (propertyEnglish === item) {
                                    route_str = route_str + "&" + otherPropertyEnglish + "=" + source_entity[item];
                                }
                            }
                        }

                        if (entityCode1 == '102' && entityType1 == '03') {
                            for (let item in entity_user) {
                                if (propertyEnglish === item) {
                                    route_str = route_str + "&" + otherPropertyEnglish + "=" + entity_user[item];
                                }
                            }
                        }

                    }
                }


            }


            let otherAddressParam = {
                companyId: userInfo.companyId,
                // entityCode: other_entityCode,
                entityCode: '001',
                // entityType: other_entityType,
                entityType: '01',
                sceneCode: '01' + userInfo.companyId + bodyInfo.entityCode + bodyInfo.entityType + '001' + userInfo.roleType + other_entityCode + other_entityType,
                // sceneCode: '30123001',
                groupEntityCode: "101",
                objectCodeType: "101"
            }

            let re2 = await this.requestMethod('/channel/channel/queryPageAddress', 'post', otherAddressParam)
            console.log('queryPageAddress：', re2)
            if (re2.data.status == 'SUCCESS') {
                let page_address1 = re2.data.data.page_address
                if (page_address1 == undefined) {
                    page_address1 = '/list/right0'
                }
                let url = page_address1 + "?" + page_str + route_str
                // let url = "?" + page_str + route_str
                urls.push(url)
            } else {
                console.log('queryPageAddress failed', re2)
                let page_address1 = '/list/right0'
                let url = page_address1 + "?" + page_str + route_str
                // let url = "?" + page_str + route_str
                urls.push(url)
            }

        }

        let addressParam = {
            companyId: userInfo.companyId,
            // entityCode: bodyInfo.entityCode,
            entityCode: '001',
            // entityType: bodyInfo.entityType,
            entityType: '01',
            sceneCode: '01' + userInfo.companyId + bodyInfo.entityCode + bodyInfo.entityType + '001' + userInfo.roleType,
            groupEntityCode: "101",
            objectCodeType: "101"
        }

        let re2 = await this.requestMethod('/channel/channel/queryPageAddress', 'post', addressParam)
        if (re2.data.status == 'SUCCESS') {
            let page_address1 = re2.data.data.page_address
            let url = page_address1
            return {
                status: 1,
                urls: urls,
                url: url
            }
        } else {
            console.log('queryPageAddress failed', re2)
            let url = '/list/right'
            return {
                status: 1,
                urls: urls,
                url: url
            }
        }
    },
}